package no.hig.ekm.ircclient;

import java.awt.Cursor;
import java.awt.PopupMenu;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ResourceBundle;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.PopupFactory;

/**
 * Creates a new menubar with submenu items
 * @author Kristofers, eigil, morten
 */
@SuppressWarnings("serial")
public class MainMenu extends JMenuBar {
	private static ResourceBundle messages;
	//Main-menus
	JMenu file = new JMenu(messages.getString("mainMenuFile"));
	JMenu servermenu = new JMenu(messages.getString("mainMenuServer"));
	JMenu fonts = new JMenu(messages.getString("mainMenuFonts"));

	// File-menu items
	JMenuItem itmConnect = new JMenuItem(messages.getString("mainMenuFileConnect"));
	JMenuItem quit = new JMenuItem(messages.getString("mainMenuFileQuit"));
	// Server-menu items
	JMenuItem join = new JMenuItem(messages.getString("mainMenuServerJoin"));
	JMenuItem priv = new JMenuItem(messages.getString("mainMenuServerPriv"));
	JMenuItem nick = new JMenuItem(messages.getString("mainMenuServerNick"));
	// Create a new sub-items - Fonts
	JMenuItem defult = new JMenuItem(messages.getString("mainMenuFontsDefault"));
	JMenuItem privat = new JMenuItem(messages.getString("mainMenuFontsPrivate"));
	JMenuItem server = new JMenuItem(messages.getString("mainMenuFontsServer"));
	JMenuItem channel = new JMenuItem(messages.getString("mainMenuFontsChannel"));
	JMenuItem chatStatus = new JMenuItem(messages.getString("mainMenuFontsChatStatus"));
	JMenuItem myText = new JMenuItem(messages.getString("mainMenuFontsMyText"));
	
	
	/**
	 * Default constructor for the menu
	 */
	public MainMenu() {

		// Set the keyboard mnemonic for menu button "File"
		file.setMnemonic(1);
		// Add the "File" button to the menubar
		add(file);

		// Set a keyboard shortcut for the item
		itmConnect.setAccelerator(KeyStroke.getKeyStroke('N',
				InputEvent.CTRL_DOWN_MASK));
		itmConnect.setToolTipText(messages.getString("mainMenuFileConnectToolTip"));
		itmConnect.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				if ((MainToolbar.cd != null) && MainToolbar.cd.isVisible()) {
					MainToolbar.cd.toFront();
				}
				else {
					setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
					MainToolbar.cd = new ConnectionDialog();
					MainWindow.md.add(MainToolbar.cd);
					ConnectionDialog.setNetworkList(IniParser.nets);
					setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
				}
			}
		});
		
		
		// Add it as a sub-item for the menu
		file.add(itmConnect);
		file.addSeparator();
		quit.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		file.add(quit);
		
		add(servermenu);
		join.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ServerSettings cjd = new ServerSettings(ServerSettings.CHANNEL);
				Main.win.addMDIWin(cjd);
				cjd.toFront();
			}
		});
		servermenu.add(join);
		priv.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ServerSettings cjd = new ServerSettings(ServerSettings.PRIVATE);
				Main.win.addMDIWin(cjd);
				cjd.toFront();
			}
		});
		servermenu.add(priv);
		nick.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				ServerSettings cjd = new ServerSettings(ServerSettings.CHANGENICK);
				Main.win.addMDIWin(cjd);
				cjd.toFront();
			}
		});
		servermenu.add(nick);
		
		//Creating a new menu settings and an sub selection with shortcut		
		//fonts.setMnemonic(1);
		add(fonts);
		defult.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.DEFAULT);
			}
		});
		fonts.add(defult);
		privat.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.PRIVATE);
			}
		});
		fonts.add(privat);
		channel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.CHANNEL);
			}
		});
		fonts.add(channel);
		server.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.SERVER);
			}
		});
		fonts.add(server);
		chatStatus.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.CHATSTATUS);
			}
		});
		fonts.add(chatStatus);
		myText.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Fonts.editFont(Fonts.MYOWNTEXT);
			}
		});
		fonts.add(myText);		
	}
	
	public static void setMessages(ResourceBundle m) {
		MainMenu.messages = m;
	}

}
